const request = require('request')
const cheerio = require('cheerio')
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const urlJoin = require('url-join')
const Setting = require('./setting')

// const filename = path.resolve(__dirname, 'a.json')
const {sendRequest} = require('./modules/sendRequest')
const {writeTo} = require('./modules/storage')
// const {getList} = require('./hupu')

const options = {
	url: '',
	method: 'GET',
	encoding: null
}

const main = (opt) => {
	const {
		name = 'hupu',
		filename = 'abc.txt'
	} = opt
	if (!/\.(txt|md|json)$/i.test(filename)) {
		throw new Error('请输入正确的文件名以及后缀')
	}
	const fullpath = path.resolve(__dirname, filename)
	if (Setting[name]) {
		options.url = urlJoin(Setting[name].base_url, Setting[name].sub_url)
		sendRequest(options, data => {
			const {getList} = require(`./${name}`)
			const datalist = getList(data)
			// console.log(datalist)
			sendRequest({url: datalist[0].url}, function(pdata) {
				const {getImgList} = require(`./${name}`)
				getImgList(pdata)
			})
			// writeTo(fullpath, getList(data))
		})
	}
}

exports.main = main
